<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
       <el-form-item label="入库单号" prop="inboundNo">
        <el-input v-model="formData.inboundNo" placeholder="请输入入库单号" />
      </el-form-item>
      <el-form-item label="仓库id" prop="warehouseId">
        <el-input v-model="formData.warehouseId" placeholder="请输入仓库id" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="formData.warehouseName" placeholder="请输入仓库名称" />
      </el-form-item>
      <el-form-item label="采购单id" prop="purchaseOrderId">
        <el-input v-model="formData.purchaseOrderId" placeholder="请输入采购单id" />
      </el-form-item>
      <el-form-item label="采购单号" prop="purchaseOrderNo">
        <el-input v-model="formData.purchaseOrderNo" placeholder="请输入采购单号" />
      </el-form-item>
      <el-form-item label="物料id" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料id" />
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input v-model="formData.materialCode" placeholder="请输入物料编号" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model="formData.specification" placeholder="请输入规格" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="formData.model" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="formData.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="收货数" prop="receivedQuantity">
        <el-input v-model="formData.receivedQuantity" placeholder="请输入收货数" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input v-model="formData.amount" placeholder="请输入金额" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="月结" prop="monthlyPayment">
        <el-radio-group v-model="formData.monthlyPayment">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生产单id" prop="productionOrderId">
        <el-input v-model="formData.productionOrderId" placeholder="请输入生产单id" />
      </el-form-item>
      <el-form-item label="生产单号" prop="productionOrderNo">
        <el-input v-model="formData.productionOrderNo" placeholder="请输入生产单号" />
      </el-form-item>
      <el-form-item label="货号" prop="itemCode">
        <el-input v-model="formData.itemCode" placeholder="请输入货号" />
      </el-form-item>
      <el-form-item label="品名" prop="itemName">
        <el-input v-model="formData.itemName" placeholder="请输入品名" />
      </el-form-item>
      <el-form-item label="客户id" prop="customerId">
        <el-input v-model="formData.customerId" placeholder="请输入客户id" />
      </el-form-item>
      <el-form-item label="客户编号" prop="customerCode">
        <el-input v-model="formData.customerCode" placeholder="请输入客户编号" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="参数1" prop="parameter1">
        <el-input v-model="formData.parameter1" placeholder="请输入参数1" />
      </el-form-item>
      <el-form-item label="参数2" prop="parameter2">
        <el-input v-model="formData.parameter2" placeholder="请输入参数2" />
      </el-form-item>
      <el-form-item label="参数3" prop="parameter3">
        <el-input v-model="formData.parameter3" placeholder="请输入参数3" />
      </el-form-item>
      <el-form-item label="参数4" prop="parameter4">
        <el-input v-model="formData.parameter4" placeholder="请输入参数4" />
      </el-form-item>
      <el-form-item label="参数5" prop="parameter5">
        <el-input v-model="formData.parameter5" placeholder="请输入参数5" />
      </el-form-item>
      <el-form-item label="参数6" prop="parameter6">
        <el-input v-model="formData.parameter6" placeholder="请输入参数6" />
      </el-form-item>
      <el-form-item label="参数7" prop="parameter7">
        <el-input v-model="formData.parameter7" placeholder="请输入参数7" />
      </el-form-item>
      <el-form-item label="参数8" prop="parameter8">
        <el-input v-model="formData.parameter8" placeholder="请输入参数8" />
      </el-form-item>
      <el-form-item label="参数9" prop="parameter9">
        <el-input v-model="formData.parameter9" placeholder="请输入参数9" />
      </el-form-item>
      <el-form-item label="参数10" prop="parameter10">
        <el-input v-model="formData.parameter10" placeholder="请输入参数10" />
      </el-form-item>
      <el-form-item label="体积" prop="volume">
        <el-input v-model="formData.volume" placeholder="请输入体积" />
      </el-form-item>
      <el-form-item label="拼单信息" prop="combinedOrderInfo">
        <el-input v-model="formData.combinedOrderInfo" placeholder="请输入拼单信息" />
      </el-form-item>
      <el-form-item label="拼单号" prop="combinedOrderNo">
        <el-input v-model="formData.combinedOrderNo" placeholder="请输入拼单号" />
      </el-form-item>
      <el-form-item label="税率" prop="taxRate">
        <el-input v-model="formData.taxRate" placeholder="请输入税率" />
      </el-form-item>
      <el-form-item label="含税价" prop="priceWithTax">
        <el-input v-model="formData.priceWithTax" placeholder="请输入含税价" />
      </el-form-item>
      <el-form-item label="含税金额" prop="amountWithTax">
        <el-input v-model="formData.amountWithTax" placeholder="请输入含税金额" />
      </el-form-item>
      <el-form-item label="采购日期" prop="purchaseDate">
        <el-date-picker
          v-model="formData.purchaseDate"
          type="date"
          value-format="x"
          placeholder="选择采购日期"
        />
      </el-form-item>
      <el-form-item label="到货日期" prop="arrivalDate">
        <el-date-picker
          v-model="formData.arrivalDate"
          type="date"
          value-format="x"
          placeholder="选择到货日期"
        />
      </el-form-item>
      <el-form-item label="科目id" prop="subjectId">
        <el-input v-model="formData.subjectId" placeholder="请输入科目id" />
      </el-form-item>
      <el-form-item label="科目编号" prop="subjectCode">
        <el-input v-model="formData.subjectCode" placeholder="请输入科目编号" />
      </el-form-item>
      <el-form-item label="科目名称" prop="subjectName">
        <el-input v-model="formData.subjectName" placeholder="请输入科目名称" />
      </el-form-item>
      <el-form-item label="客户PO" prop="customerPo">
        <el-input v-model="formData.customerPo" placeholder="请输入客户PO" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { StockMaterialInboundApi } from '@/api/erp/stockmaterialinbound'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  inboundId: undefined,
  inboundNo: undefined,
  warehouseId: undefined,
  warehouseName: undefined,
  purchaseOrderId: undefined,
  purchaseOrderNo: undefined,
  materialId: undefined,
  materialCode: undefined,
  materialName: undefined,
  specification: undefined,
  model: undefined,
  unit: undefined,
  receivedQuantity: undefined,
  unitPrice: undefined,
  amount: undefined,
  remark: undefined,
  monthlyPayment: undefined,
  productionOrderId: undefined,
  productionOrderNo: undefined,
  itemCode: undefined,
  itemName: undefined,
  customerId: undefined,
  customerCode: undefined,
  customerName: undefined,
  parameter1: undefined,
  parameter2: undefined,
  parameter3: undefined,
  parameter4: undefined,
  parameter5: undefined,
  parameter6: undefined,
  parameter7: undefined,
  parameter8: undefined,
  parameter9: undefined,
  parameter10: undefined,
  volume: undefined,
  combinedOrderInfo: undefined,
  combinedOrderNo: undefined,
  taxRate: undefined,
  priceWithTax: undefined,
  amountWithTax: undefined,
  purchaseDate: undefined,
  arrivalDate: undefined,
  subjectId: undefined,
  subjectCode: undefined,
  subjectName: undefined,
  customerPo: undefined
})
const formRules = reactive({
  inboundId: [{ required: true, message: '入库单号ID不能为空', trigger: 'blur' }],
  inboundNo: [{ required: true, message: '入库单号不能为空', trigger: 'blur' }],
  warehouseId: [{ required: true, message: '仓库id不能为空', trigger: 'blur' }],
  warehouseName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }],
  purchaseOrderId: [{ required: true, message: '采购单id不能为空', trigger: 'blur' }],
  purchaseOrderNo: [{ required: true, message: '采购单号不能为空', trigger: 'blur' }],
  materialId: [{ required: true, message: '物料id不能为空', trigger: 'blur' }],
  materialCode: [{ required: true, message: '物料编号不能为空', trigger: 'blur' }],
  materialName: [{ required: true, message: '物料名称不能为空', trigger: 'blur' }],
  specification: [{ required: true, message: '规格不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, inboundId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.inboundId = inboundId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StockMaterialInboundApi.getStockMaterialInboundDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await StockMaterialInboundApi.createStockMaterialInboundDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await StockMaterialInboundApi.updateStockMaterialInboundDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    inboundId: undefined,
    inboundNo: undefined,
    warehouseId: undefined,
    warehouseName: undefined,
    purchaseOrderId: undefined,
    purchaseOrderNo: undefined,
    materialId: undefined,
    materialCode: undefined,
    materialName: undefined,
    specification: undefined,
    model: undefined,
    unit: undefined,
    receivedQuantity: undefined,
    unitPrice: undefined,
    amount: undefined,
    remark: undefined,
    monthlyPayment: undefined,
    productionOrderId: undefined,
    productionOrderNo: undefined,
    itemCode: undefined,
    itemName: undefined,
    customerId: undefined,
    customerCode: undefined,
    customerName: undefined,
    parameter1: undefined,
    parameter2: undefined,
    parameter3: undefined,
    parameter4: undefined,
    parameter5: undefined,
    parameter6: undefined,
    parameter7: undefined,
    parameter8: undefined,
    parameter9: undefined,
    parameter10: undefined,
    volume: undefined,
    combinedOrderInfo: undefined,
    combinedOrderNo: undefined,
    taxRate: undefined,
    priceWithTax: undefined,
    amountWithTax: undefined,
    purchaseDate: undefined,
    arrivalDate: undefined,
    subjectId: undefined,
    subjectCode: undefined,
    subjectName: undefined,
    customerPo: undefined
  }
  formRef.value?.resetFields()
}
</script>